%load_ext autoreload
%autoreload 2
import os
import numpy as np
from matplotlib import pyplot as plt
from scipy import ndimage
from pySurf.data2D_class import Data2D
from pySurf.data2D import plot_data
from dataIO.fn_add_subfix import fn_add_subfix
from pySurf.fit_cylinder import fit_cylinder, fit_cone
from pySurf.points import resample_grid, subtract_points, plot_points, points_autoresample
from pySurf.readers.instrumentReader import matrix_reader, matrix4D_reader
from pySurf.scripts.dlist import Dlist,load_dlist
from plotting.backends import maximize
from dataIO.span import span
import inspect
import warnings
#warnings.filterwarnings('ignore')
np.set_printoptions(threshold=40) #truncate arrays longer than 40
%reset
Voglio fare vedere come usare questa libreria.
L'oggetto principale e' Data2D, rappresenta una superficie con assi associati.
E' una libreria python, quindi ha tutte le feature del python, ad posso ispezionarne gli oggetti con ?.
Data2D?
Come vediamo puo' essere inizializzato con data,x,y e tante altre cose. Tipicamente pero' vorremo leggere dati da qualche file. C'e' un'argomento reader per selezionare una funzione per leggere i dati, ve ne sono per molti comuni strumenti e formati, o puo' essere implementata custom. Se il reader non e' passato esplicitamente, prova ad indovinare il formato, ed in genere ci azzecca abbastanza.
fn = r'G:\My Drive\progetti\ion_beam\specchio_beatrix\exemplar_data\MFT-txt\EN4-2-100.txt'
fn2 = r'G:\My Drive\progetti\c_overcoating\esperimenti\20200214_batch2_IrC\20200306_MFT_calibration\01_18803_A.csv'
d = Data2D(fn2)
print ("Return object", d)
Posso provare ad elencare i metodi della classe (ad esempio, esiste un metodo plot, cosa fara'?!):
print([d for d in dir(Data2D) if d[:2] != '__'])
Quindi proviamo a leggere qualche dato (salvato da MFT in formato testo):
d.plot()
Notiamo tuttavia che le unita' degli assi sono scomode (ed ignote), vorrei anche collocare il centro dell'immagine sull'origine (potrebbe pero' essere ovunque). I can also invert y axis to match format specs:
d = Data2D(fn2,units=['mm','mm','nm'],center=(0,0),scale=(1000,-1000,1)) #this matches exactly Gwyddion
d.plot()
This is an example of how I can read a simple matrix csv file, passed by a colleague, that was stripped of the header and has an arbitrary delimiter, scale (correct by wavelength), etc.
d2 = Data2D(fn,units=['mm','mm','nm'],center=(0,0),matrix=1,delimiter='',scale=(0.001,0.001,635.)) #this matches exactly MFT software
d2.plot()
Riguardo ai plot (ed in generale quando possibile agli altri metodi), posso passare normali argomenti python, ad es. cambiare titolo e assi. Posso salvare la figura con i normali metodi python (quindi di conseguenza molte soluzioni si trovano su stackoverflow). Ad esempio per cambiare palette e scala colori:
d.plot(vmin=-2,vmax=+2,cmap=plt.get_cmap('jet'))
plt.title('surface of data')
The returned object represents a set of data and axis. These can be returned calling the object itself:
d()
But can also be represented with x,y,z points:
d.topoints()
Functions (not yet classes) to manipulate data points in 3-coordinate format are available in module points.
Qualche semplice analisi (in realta' la funzione e' ancora un po' grezza, mandatemi pure feedback e richieste).
h1 = d.histostats()
Se proprio vogliamo strafare (in realta' la funzione e' ancora un po' grezza, mandatemi pure feedback e richieste).
h1 = d.histostats()
h2 = d.remove_outliers(nsigma=1).histostats() #i picchi vengono fuori dal non aver lasciato gli stessi bin di prima, ma perche'?
plt.figure()
d.histostats(bins=h2[1]) #ci sarebbero anche su h1 se plottato con stessi bins
dd = d.remove_outliers(nsigma=1)
dd.plot()
Ci sono funzionalita' piu' complicate, tipo iterare o livellare.
%qtconsole
res=d.copy()
res.data
To show some complex analysis:
d.psd(analysis=True,title='')
plt.show()
Spiego i tre pannelli. Notare le unita' di misura (questo perche' avevo definito units in inizializzazione). Pero' puo' essere migliorato. I don't like the color range of PSD.
d.psd(analysis=True,title='',prange=[1e-5,None])
plt.show()
d.psd(analysis=True,title='',prange=[1e-5,None],rmsrange=[[None,0.1],[0.1,100],[100,None]])
plt.show()
Notare valori rms per banda sopra grafico.